package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.RecommendInfo;
import com.macro.mall.service.RecommendInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * RecommendInfo的路由接口服务
 *
 * @author
 */
@Slf4j
@RestController
@RequestMapping("RecommendInfoController")
@Api(tags = "Recommend", description = "推荐管理")
public class RecommendInfoController {

    /**
     * RecommendInfoService服务
     */
    @Autowired
    private RecommendInfoService recommendInfoService;

    /**
     * 查询所有RecommendInfo数据的方法
     *
     * @param
     * @return
     */
    @ApiOperation("查询列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult find(@ApiParam(value = "根据type存放对应id") @RequestParam(value = "sourceId", required = false) Integer sourceId,
                             @ApiParam(value = "01-课程 02-服务 03-新闻 04-广告 05-居委会 06-志愿者(如查询多种类型 ',' 分隔)") @RequestParam(value = "sourceType", required = false) String sourceType,
                             @ApiParam(value = "1-APP 2-小程序 3-公众号") @RequestParam(value = "channel", required = false) String channel,
                             @ApiParam(value = "名称") @RequestParam(value = "name", required = false) String name,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return CommonResult.success(CommonPage.restPage(recommendInfoService.find(sourceId, sourceType, channel, name, pageNum, pageSize)));
    }

    /**
     * 通过id查询RecommendInfo数据的方法
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查询")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public CommonResult findOne(@RequestParam(name = "id") Integer id) {
        return CommonResult.success(recommendInfoService.findOne(id));
    }

    /**
     * 插入RecommendInfo属性不为空的数据方法
     *
     * @param value
     * @return
     */
    @ApiOperation("保存")
    @PostMapping(value = "/save", produces = {"application/json;charset=UTF-8"})
    public CommonResult save(@RequestBody RecommendInfo value) {
        return CommonResult.success(recommendInfoService.saveNotNull(value));
    }

    /**
     * 更新RecommendInfo属性不为空的数据方法
     *
     * @param value
     * @return
     */
    @ApiOperation("修改")
    @PostMapping(value = "/update", produces = {"application/json;charset=UTF-8"})
    public CommonResult update(@RequestBody RecommendInfo value) {
        return CommonResult.success(recommendInfoService.updateNotNullById(value));
    }

    /**
     * 通过id删除RecommendInfo数据方法
     *
     * @param id
     * @return
     */
    /**01-课程 02-服务 03-新闻 04-广告*/
    @ApiModelProperty(value = "01-课程 02-服务 03-新闻 04-广告")
    private String sourceType;
    @ApiOperation("删除")
    @GetMapping(value = "/delete", produces = {"application/json;charset=UTF-8"})
    public CommonResult delete(@RequestParam(name = "sourceId") Integer sourceId, @RequestParam(name = "sourceType")String sourceType) {
        return CommonResult.success(recommendInfoService.deleteById(sourceId,sourceType));
    }
}
